﻿#pragma once
#pragma once
#include<iostream>
#include<vector>
using namespace std;
enum Color {
	RED,
	BLACK
};

template<class T>
struct RBTreeNode
{
	T _data;
	RBTreeNode<T>* _parent;
	RBTreeNode<T>* _left;
	RBTreeNode<T>* _right;
	Color _col;

	RBTreeNode(const T& data)
		:_parent(nullptr)
		, _left(nullptr)
		, _right(nullptr)
		, _data(data)
	{}
};

template<class T,class Ref, class Ptr>
struct RBTreeIterator
{
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator <T,Ref,Ptr> Self;
	Node* _node;
	Node* _root;
	RBTreeIterator(Node* node,Node* root)
		:_node(node)
		,_root(root)
	{}
	Ref operator*()
	{
		return _node->_data;
	}
	Ptr operator->()
	{
		return &_node->_data;
	}

	Self& operator++()
	{ 
		Node* cur = _node;
		if (cur->_right)
		{
			//右子树不为空，找左子树的最左节点
			Node* Leftmost = cur->_right;
			while (Leftmost&&Leftmost->_left)
			{
				Leftmost = Leftmost->_left;
			}
			_node = Leftmost;
		}
		else
		{
			//找到父亲左孩子是cur的节点
			Node* parent = cur->_parent;
			while (parent && cur==parent->_right)
			{
			
					cur = parent;
					parent = cur->_parent;
			}
			_node = parent;
		}
		return *this;
	}

	Self& operator--()
	{
		if (_node == nullptr)
		{
			//找右子树的最右节点(中序的最后一个节点)
			Node* RightMost = _root;
			while (RightMost && RightMost->_right)
			{
				RightMost = RightMost->_right;
			}
			_node = RightMost;
		}
		else if (_node->_left)
		{
			//左不为空，找左子树的最右节点
			Node* Rightmost = _node->_left;
			while (Rightmost->_right)
			{
				Rightmost = Rightmost->_right;
			}
			_node = Rightmost;
		}
		else
		{
			//找孩子是父亲右边的节点
			Node* cur = _node;
			Node* parent = cur->_parent;
			while (parent && cur == parent->_left)
			{
				cur = parent;
				parent = parent->_parent;
			}
			_node = parent;
		}
		return *this;
	}


	bool operator!=(const Self& it) const
	{
		return _node != it._node;
	}

	bool operator==(const Self& it) const
	{
		return _node == it._node;
	}
};

template <class K, class T,class KeyofT>
class RBTree
{
public:
	typedef RBTreeNode<T> Node;
	typedef RBTreeIterator<T,T&,T*> Iterator;
	typedef RBTreeIterator<T, const T&, const T*> ConstIterator;
	KeyofT kot;

	Iterator Begin()
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return Iterator(cur,_root);
	}

	Iterator End()
	{
		return Iterator(nullptr,_root);
	}
 	
	ConstIterator Begin()const
	{
		Node* cur = _root;
		while (cur && cur->_left)
		{
			cur = cur->_left;
		}
		return ConstIterator(cur,_root);
	}

	ConstIterator End()const
	{
		return ConstIterator(nullptr,_root);
	}
	RBTree() = default;
	
	~RBTree()
	{
		Destroy(_root);
		_root = nullptr;
	}

	RBTree(const RBTree& t)
	{
		_root = Copy(t._root);
	}

	RBTree& operator=(RBTree t)
	{
		swap(_root, t._root);
		return *this;
	}
	
	Iterator Find(const T& data)
	{
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) < kot(data))
			{
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				cur = cur->_left;
			}
			else
			{
				return Iterator(cur, _root);
			}
		}
		return End();
	}

	

	void RotateR(Node* parent)
	{
		Node* subL = parent->_left;
		Node* subLR = subL->_right;
		parent->_left = subLR;
		if (subLR)
			subLR->_parent = parent;

		Node* Pparent = parent->_parent;
		subL->_right = parent;
		parent->_parent = subL;

		if (parent == _root)
		{
			_root = subL;
			subL->_parent = nullptr;
		}
		else
		{
			if (parent == Pparent->_left)
				Pparent->_left = subL;
			else
				Pparent->_right = subL;
		}
		subL->_parent = Pparent;
	}


	void RotateL(Node* parent)
	{
		Node* subR = parent->_right;
		Node* subRL = subR->_left;
		parent->_right = subRL;
		if (subRL)
			subRL->_parent = parent;
		Node* Pparent = parent->_parent;

		subR->_left = parent;
		parent->_parent = subR;

		if (parent == _root)
		{
			_root = subR;
			subR->_parent = nullptr;
		}
		else
		{
			if (parent == Pparent->_left)
				Pparent->_left = subR;
			else
				Pparent->_right = subR;
		}
		subR->_parent = Pparent;
	}


	pair<Iterator,bool> insert(const T& data)
	{
		if (_root == nullptr)
		{
			_root = new Node(data);
			_root->_col = BLACK;
			//return {Iterator(_root,_root),true};
			return pair<Iterator, bool>{Iterator(_root, _root), true};

		}
		Node* parent = nullptr;
		Node* cur = _root;
		while (cur)
		{
			if (kot(cur->_data) < kot(data))
			{
				parent = cur;
				cur = cur->_right;
			}
			else if (kot(cur->_data) > kot(data))
			{
				parent = cur;
				cur = cur->_left;
			}
			else
			{
				return { Iterator(cur,_root),true };
			}
		}
		cur = new Node(data);
		Node* newnode = cur;
		// 新增节点颜色为红色
		cur->_col = RED;
		if (kot(parent->_data) < kot(data))
		{
			parent->_right = cur;
		}
		else
		{
			parent->_left = cur;
		}
		cur->_parent = parent;
		//更新颜色
		while (parent && parent->_col == RED)
		{
			Node* grandfather = parent->_parent;
			//grandfather 

		//  parent     uncle

			if (parent == grandfather->_left)
			{
				Node* uncle = grandfather->_right;
				//uncle 存在并且为红色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}

				//uncle不存在或者uncle为黑色
				else
				{

					// grandfather 

				//  parent     uncle

			// cur
					if (cur == parent->_left)
					{
						//对grandfather右单旋+变色
						RotateR(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}

					// grandfather 

				//parent        uncle

					//cur
					else
					{
						//双旋+变色
						//对父亲节点左单旋
						RotateL(parent);
						//爷爷节点右单旋
						RotateR(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}
			}
			//gfather 
	  //uncle         parent   
			else
			{
				Node* uncle = grandfather->_left;
				//叔叔存在且为红色
				if (uncle && uncle->_col == RED)
				{
					parent->_col = uncle->_col = BLACK;
					grandfather->_col = RED;

					cur = grandfather;
					parent = cur->_parent;
				}

				//叔叔不存在或者为黑色
				else
				{
					// 情况⼆：叔叔不存在或者存在且为⿊
					// 旋转+变⾊
						// g
					  // u   p
							// c
					if (cur == parent->_right)
					{
						RotateL(grandfather);
						parent->_col = BLACK;
						grandfather->_col = RED;
					}
					else
					{
						RotateR(parent);
						RotateL(grandfather);
						cur->_col = BLACK;
						grandfather->_col = RED;
					}
					break;
				}

			}
		}
		_root->_col = BLACK;
		return pair<Iterator, bool>{Iterator(newnode, _root), true};
	}

	//bool Insert(const pair<K, V>& kv)
	//{
	//	if (_root == nullptr)
	//	{
	//		_root = new Node(kv);
	//		_root->_col = BLACK;
	//		return true;
	//	}
	//	Node* parent = nullptr;
	//	Node* cur = _root;
	//	while (cur)
	//	{
	//		if (cur->_kv.first < kv.first)
	//		{
	//			parent = cur;
	//			cur = cur->_right;
	//		}
	//		else if (cur->_kv.first > kv.first)
	//		{
	//			parent = cur;
	//			cur = cur->_left;
	//		}
	//		else
	//		{
	//			return false;
	//		}
	//	}
	//	cur = new Node(kv);
	//	// 新增结点。颜⾊红⾊给红⾊
	//	cur->_col = RED;
	//	if (parent->_kv.first < kv.first)
	//	{
	//		parent->_right = cur;
	//	}
	//	else
	//	{
	//		parent->_left = cur;
	//	}
	//	cur->_parent = parent;
	//	while (parent && parent->_col == RED)
	//	{
	//		Node* grandfather = parent->_parent;
	//		// g
	//		// p u
	//		if (parent == grandfather->_left)
	//		{
	//			Node* uncle = grandfather->_right;
	//			if (uncle && uncle->_col == RED)
	//			{
	//				// u存在且为红 -》变⾊再继续往上处理
	//				parent->_col = uncle->_col = BLACK;
	//				grandfather->_col = RED;
	//				cur = grandfather;
	//				parent = cur->_parent;
	//			}
	//			else
	//			{
	//				// u存在且为⿊或不存在 -》旋转+变⾊
	//				if (cur == parent->_left)
	//				{
	//					// g
	//					// p u
	//					//c
	//					//单旋
	//					RotateR(grandfather);
	//					parent->_col = BLACK;
	//					grandfather->_col = RED;
	//				}
	//				else
	//				{
	//					// g
	//					// p u
	//					// c
	//					//双旋
	//					RotateL(parent);
	//					RotateR(grandfather);
	//					cur->_col = BLACK;
	//					grandfather->_col = RED;
	//				}
	//				break;
	//			}
	//		}
	//		else
	//		{
	//			// g
	//			// u p
	//			Node* uncle = grandfather->_left;
	//			// 叔叔存在且为红，-》变⾊即可
	//			if (uncle && uncle->_col == RED)
	//			{
	//				parent->_col = uncle->_col = BLACK;
	//				grandfather->_col = RED;
	//				// 继续往上处理
	//				cur = grandfather;
	//				parent = cur->_parent;
	//			}
	//			else // 叔叔不存在，或者存在且为⿊
	//			{
	//				// 情况⼆：叔叔不存在或者存在且为⿊
	//				// 旋转+变⾊
	//				// g
	//				// u p
	//				// c
	//				if (cur == parent->_right)
	//				{
	//					RotateL(grandfather);
	//					parent->_col = BLACK;
	//					grandfather->_col = RED;
	//				}
	//				else
	//				{
	//					// g
	//					// u p
	//					// c
	//					RotateR(parent);
	//					RotateL(grandfather);
	//					cur->_col = BLACK;
	//					grandfather->_col = RED;
	//				}
	//				break;
	//			}
	//		}
	//	}
	//	_root->_col = BLACK;
	//	return true;
	//}

	Node* find(const K& key)
	{
		Node* cur = _root;
		while (cur)
		{
			if (cur->_kv.first < key)
			{
				cur = cur->_right;
			}
			else if (cur->_kv.first > key)
			{
				cur = cur->_left;
			}
			else
			{
				return cur;
			}
		}
		return nullptr;
	}

	
	int Height()
	{
		return _Height(_root);
	}
	int Size()
	{
		return _Size(_root);
	}
	

private:
	Node* _root = nullptr;
	int _Height(Node* root)
	{
		if (root == nullptr)
			return 0;
		int leftHeight = _Height(root->_left);
		int rightHeight = _Height(root->_right);
		return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
	}
	int _Size(Node* root)
	{
		if (root == nullptr)
		{
			return 0;
		}
		return _Size(root->_left) + _Size(root->_right) + 1;
	}
	void Destroy(Node* root)
	{
		if (root == nullptr)
			return;
		Destroy(root->_left);
		Destroy(root->_right);
		delete root;
	}
	Node* Copy(Node* root)
	{
		if (root == nullptr)
			return nullptr;
		Node* newRoot = new Node(root->_data);
		newRoot->_left = Copy(root->_left);
		newRoot->_right = Copy(root->_right);
		return newRoot;

	}

};